import { createSlice } from "@reduxjs/toolkit";
import {req} from "@/utils";
import { getToken,setToken as _setToken } from "../../utils";
const userStore = createSlice({
    name:"user",
    initialState:{
      token:getToken() || ''
    },
    reducers:{
      setToken(state,action){
        state.token = action.payload
        _setToken(action.payload)
      }
    }
})

const { setToken } = userStore.actions;
const userReducer = userStore.reducer;


const fetchLogin = (loginForm)=>{
  return async(dispatch)=>{
    const res = await req.post('/login',loginForm)
    console.log(res);
    dispatch(setToken(res))
    return res;
  }
}

const fetchsendVerification = (loginForm)=>{
  return async(dispatch)=>{
    const res = await req.post('/registerVerification',loginForm)
   return res;
    
  }
}
const fetchregister = (loginForm)=>{
  return async(dispatch)=>{
    const res = await req.post('/register',loginForm)
    return res;
  }
}

const fetchUserInfo = ()=>{
  return async(dispatch)=>{
    const res = await req.get('/user/profile')
    return res;
  }
}
const fetchUserProfile = (id)=>{
  return async(dispatch)=>{
    const res = await req.get(`/user/${id}/profile`)
    
    return res;
  }
}
const modifyUserInfo = (values)=>{
  return async(dispatch)=>{
    const res = await req.put('/user/profile',values)
    return res;
  }
}

const fetchresetVerification = ()=>{
  return async(dispatch)=>{
    const res = await req.post('/user/resetVerification')
    return res;
  }
}
const modifypassword = (values)=>{
  return async(dispatch)=>{
    const res = await req.post('/user/password',values)
    return res;
  }
}

const uploadAvatar = (fromdata)=>{
  return async(dispatch)=>{
    const res = await req.post('/user/uploadAvatar',fromdata)
    return res;
  }
}

const downloadTorrent = (data)=>{
  return async(dispatch)=>{
    const res = await req.post('/tracker/downloadTorrent',data)
   return res;
    
  }
}

// 关注用户
 const followUser = (userId) => async () => {
  const res = await req.post(`/user/follow/${userId}`);
  return res;
};

// 取消关注用户
 const deFollowUser = (userId) => async () => {
  const res = await req.post(`/user/deFollow/${userId}`);
  return res;
};

const fetchTorrentList = (params = {}) => async (dispatch) => {
  try {
    const { id, page = 1, size = 10 } = params;
    let response;
    if (id) {
      // 单个种子查询
      response = await req.post('/tracker/getTorrentList', { id });
    } else {
      // 分页查询
      response = await req.post('/tracker/getTorrentList', {}, {
        params: { page, size }
      });
    }
    
    if (response.code === 0) {
      return response;
    }
    throw new Error(response.msg || '获取种子列表失败');

  } catch (err) {
    console.error('获取种子列表错误:', err);
    throw err;
  }
};


const uploadTorrent = (formdata)=>{
  return async(dispatch)=>{
    const res = await req.post('/tracker/uploadTorrent',formdata)
    return res;
  }
}
const createTorrent = (formdata)=>{
  return async(dispatch)=>{
    const res = await req.post('/tracker/createTorrent',formdata)
    return res;
  }
}


const fetchComments = (params = {})=>{
  return async(dispatch)=>{
    const res = await req.get('/api/seeds/getCommentList',{ params })
    return res;
  }
}


const addComment = (data)=>{
  return async(dispatch)=>{
    console.log("addComment:", data);
    const res = await req.post('/api/seeds/comment',data)
    return res;
  }
}
const likeComment = (data)=>{
  return async(dispatch)=>{
    console.log("likeComment:", data);
    const res = await req.post('/api/seeds/likes',data);
    console.log("likeComment return:", res);
    return res;
  }
}

const deleteComment = (data)=>{
  return async(dispatch)=>{
    console.log("deleteComment:", data);
    const res = await req.post('/api/seeds/deleteComment',data)
    return res;
  }
}


export{ setToken ,fetchregister,fetchsendVerification,fetchLogin,fetchUserInfo,modifyUserInfo,
  modifypassword,fetchresetVerification,uploadAvatar,fetchTorrentList,fetchUserProfile,followUser,
  deFollowUser,uploadTorrent,downloadTorrent,createTorrent,fetchComments,addComment,likeComment,deleteComment}
export default userReducer